package com.dfdchem.imd.kaiwu.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.dfdchem.imd.kaiwu.R;
import com.dfdchem.imd.kaiwu.activity.base.BaseActivityAnon;
import com.dfdchem.imd.kaiwu.constant.RouterPath;
import com.dfdchem.imd.kaiwu.interfaces.OnHttpResponseListener;
import com.dfdchem.imd.kaiwu.manager.DataManager;
import com.dfdchem.imd.kaiwu.model.Resp;
import com.dfdchem.imd.kaiwu.model.User;
import com.dfdchem.imd.kaiwu.util.JSON;
import com.dfdchem.imd.kaiwu.util.RegexUtil;
import com.dfdchem.imd.kaiwu.util.RequestUtil;
import com.google.android.material.tabs.TabLayout;

import zuo.biao.library.manager.SystemBarTintManager;
import zuo.biao.library.util.Log;

@Route(path = RouterPath.LOGIN_ACTIVITY)
public class LoginActivity extends BaseActivityAnon implements TabLayout.BaseOnTabSelectedListener, View.OnClickListener, OnHttpResponseListener {
    private static final String TAG = "LoginActivity";

    private Button login;
    private Button forget_password;
    private Button register;
    private Button setting;
    private int tabIndex = 0;
    private TabLayout tabLayout;

    private EditText login_form_account;
    private EditText login_form_password;

    private LinearLayout login_from_phone_layout;
    private LinearLayout login_from_pwd_layout;

    private static final int request_code_login = 1;


    protected int enterAnim = R.anim.bottom_push_in;

    protected int exitAnim = R.anim.bottom_push_out;

    /**
     * 启动这个Activity的Intent
     * @param context
     * @return Intent
     */
    public static Intent createIntent(Context context) {
        Intent intent = new Intent(context, LoginActivity.class);
        return intent;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        initView();
        initData();
        initEvent();
    }

    public void initView() {
        Log.d(TAG, this.getClass().getSimpleName() + " initView");
        forget_password = findViewById(R.id.forget_password);
        register = findViewById(R.id.register);
        login = findViewById(R.id.login);
        setting = findViewById(R.id.setting);
        tabLayout = findViewById(R.id.tabLayout);
        login_from_phone_layout = findViewById(R.id.login_from_phone_layout);
        login_from_pwd_layout = findViewById(R.id.login_from_pwd_layout);

        login_form_account = findViewById(R.id.login_form_account);
        login_form_password = findViewById(R.id.login_form_password);

        SystemBarTintManager tintManager = new SystemBarTintManager(this);
        tintManager.setStatusBarTintEnabled(true);
        tintManager.setStatusBarTintResource(R.color.colorPrimaryDark);
    }

    public void initData() {
        Log.d(TAG, this.getClass().getSimpleName() + " initData");
        DataManager.getInstance().putXToken(null);
    }

    public void initEvent() {
        Log.d(TAG, this.getClass().getSimpleName() + " initEvent");

        forget_password.setOnClickListener(this);
        register.setOnClickListener(this);
        login.setOnClickListener(this);
        setting.setOnClickListener(this);
        tabLayout.addOnTabSelectedListener(this);
    }

    private User formCheck(){
        String account = login_form_account.getEditableText().toString();
        String password = login_form_password.getEditableText().toString();

        if (account.length() != 11) {
            Toast.makeText(this, "手机号长度不正确", Toast.LENGTH_SHORT).show();
            return null;
        } else if (!RegexUtil.isPhone(account)) {
            Toast.makeText(this, "手机号格式不正确，请重新输入", Toast.LENGTH_SHORT).show();
            return null;
        }
        if (password.length() < 6) {
            Toast.makeText(this, "密码长度不能小于6", Toast.LENGTH_SHORT).show();
            return null;
        }

        User user = new User();
        user.setPhone(account);
        user.setPassword(password);

        return user;
    }

    @Override
    public void onTabSelected(TabLayout.Tab tab) {
        Log.d(TAG, "onTabSelected:" + tab.getPosition());

        int position = tab.getPosition();
        if (position < 0 || position > 1) {
            return;
        }

        if (position == 0) {
            Toast.makeText(this, "暂不支持验证码登录", Toast.LENGTH_SHORT).show();
            /*
            login_from_phone_layout.setVisibility(View.VISIBLE);
            login_from_pwd_layout.setVisibility(View.GONE);
            */
        } else {
            login_from_phone_layout.setVisibility(View.GONE);
            login_from_pwd_layout.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void onTabUnselected(TabLayout.Tab tab) {
        Log.d(TAG, "onTabUnselected:" + tab.getPosition());
    }

    @Override
    public void onTabReselected(TabLayout.Tab tab) {
        Log.d(TAG, "onTabReselected:" + tab.getPosition());
    }

    public void onCloseClick(View v) {
        Log.d(TAG, "onCloseClick >>>");
        finish();
        overridePendingTransition(zuo.biao.library.R.anim.bottom_push_out, zuo.biao.library.R.anim.bottom_push_in);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {

            case R.id.forget_password:
                Toast.makeText(this, "请联系管理员", Toast.LENGTH_SHORT).show();
                break;
            case R.id.register:
                toActivity(RouterPath.REGISTER_ACTIVITY);
                break;
            case R.id.setting:
                toActivity(RouterPath.ENV_SETTING_ACTIVITY);
                break;
            case R.id.login:
                login.setEnabled(false);
                final User user = formCheck();
                if (user == null) {
                    login.setEnabled(true);
                    return;
                }
                showProgressDialog("登录中...");
                    runThread(TAG + "register", new Runnable() {
                        @Override
                        public void run() {
                            RequestUtil.login(user, request_code_login, LoginActivity.this);
                        }
                });
                break;
            default:
                break;
        }
    }

    @Override
    public void onHttpResponse(int requestCode, String resultJson, Exception e) {
        if (e != null) {
            Toast.makeText(this, "请求失败，" + e.getMessage(), Toast.LENGTH_LONG).show();
            return;
        }
        switch (requestCode) {
            case request_code_login:
                login.setEnabled(true);
                dismissProgressDialog();
                Resp<String> resp = JSON.parseResp(resultJson);
                if (resp == null || resp.getCode() != 200) {
                    Toast.makeText(this, resp != null ? resp.getMsg() : "请求失败，请重试", Toast.LENGTH_SHORT).show();
                    return;
                }
                String x_token = resp.getData();
                DataManager.getInstance().putXToken(x_token);

                toActivity(RouterPath.MAIN_ACTIVITY, true, 0, zuo.biao.library.R.anim.bottom_push_out);
                break;
        }
    }
}